package com.ikun.aimian.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikun.aimian.common.ErrorCode;
import com.ikun.aimian.exception.ThrowUtils;
import com.ikun.aimian.mapper.UserFavoriteMapper;
import com.ikun.aimian.model.dto.userfavour.UserFavoriteQueryRequest;
import com.ikun.aimian.model.entity.App;
import com.ikun.aimian.model.entity.User;
import com.ikun.aimian.model.entity.UserFavorite;
import com.ikun.aimian.model.vo.UserFavoriteVO;
import com.ikun.aimian.service.AppService;
import com.ikun.aimian.service.UserFavoriteService;
import com.ikun.aimian.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class UserFavoriteServiceImpl extends ServiceImpl<UserFavoriteMapper, UserFavorite> implements UserFavoriteService {

    @Resource
    private UserService userService;

    @Resource
    private AppService appService;

    @Override
    public void validUserFavorite(UserFavorite favorite, boolean add) {
        ThrowUtils.throwIf(favorite == null, ErrorCode.PARAMS_ERROR);
        Long appId = favorite.getAppId();
        Long userId = favorite.getUserId();
        if (add) {
            ThrowUtils.throwIf(appId == null || appId <= 0, ErrorCode.PARAMS_ERROR, "应用ID不能为空");
            ThrowUtils.throwIf(userId == null || userId <= 0, ErrorCode.PARAMS_ERROR, "用户ID不能为空");
        }
        // 验证应用是否存在
        if (appId != null) {
            App app = appService.getById(appId);
            ThrowUtils.throwIf(app == null, ErrorCode.PARAMS_ERROR, "应用不存在");
        }
    }

    @Override
    public QueryWrapper<UserFavorite> getQueryWrapper(UserFavoriteQueryRequest queryRequest) {
        QueryWrapper<UserFavorite> queryWrapper = new QueryWrapper<>();
        if (queryRequest == null) return queryWrapper;
        if (queryRequest.getAppId() != null) {
            queryWrapper.eq("appId", queryRequest.getAppId());
        }
        if (queryRequest.getUserId() != null) {
            queryWrapper.eq("userId", queryRequest.getUserId());
        }
        // 默认按收藏时间倒序
        queryWrapper.orderByDesc("createTime");
        return queryWrapper;
    }

    @Override
    public UserFavoriteVO getUserFavoriteVO(UserFavorite favorite, HttpServletRequest request) {
        if (favorite == null) return null;
        UserFavoriteVO vo = UserFavoriteVO.fromEntity(favorite);
        // 补充用户信息
        Long userId = favorite.getUserId();
        if (userId != null) {
            User user = userService.getById(userId);
            if (user != null) {
                vo.setUserName(user.getUserName());
            }
        }
        // 补充应用信息
        Long appId = favorite.getAppId();
        if (appId != null) {
            App app = appService.getById(appId);
            if (app != null) {
                vo.setAppName(app.getAppName());
                // 如有需要，也可以设置 appIcon
            }
        }
        return vo;
    }

    @Override
    public List<UserFavoriteVO> getUserFavoriteVOListByUserId(Long userId) {
        QueryWrapper<UserFavorite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.orderByDesc("createTime");
        List<UserFavorite> list = list(queryWrapper);
        if (CollUtil.isEmpty(list)) {
            return null;
        }
        return list.stream()
                .map(favorite -> getUserFavoriteVO(favorite, null))
                .collect(Collectors.toList());
    }
}
